<template>
  <div v-if="tableData.length">
    <div class="wrapper" v-for="(item, index) in tableData" :key="item.id">
      <div class="user">
        <el-avatar class="avatar" :size="50" :src="item.useravatar"></el-avatar>
        <p class="username">{{ item.nikeName }}</p>
      </div>
      <input :id="index" class="exp" type="checkbox" />
      <div class="text">
        <label class="btn" :for="index"></label>
        {{ item.content }}
      </div>
    </div>
    <div style="display: flex; justify-content: center">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
  <div v-else class="nullCss">暂无评论~</div>
</template>


<script>
import request from "@/utils/request";
export default {
  data() {
    return {
      total: 0,
      pageNum: 1,
      pageSize: 5,
      tableData: [],
      Index: 1,
      isActive: 1,
      usercomment: [],
      //获取localStorage中保存的后台返回的用户信息
      user: localStorage.getItem("user")
        ? JSON.parse(localStorage.getItem("user"))
        : {},
    };
  },
  created() {
    this.load();
  },
  methods: {
    load() {
      const id = this.$route.query.id;
      let username = null;
      if (id != null) {
        // 使用 async/await
        this.getUserNameById(id).then((name) => {
          username = name;
          this.requestComment(username);
        });
      } else {
        username = this.user.userName;
        this.requestComment(username);
      }
    },
    async getUserNameById(id) {
      const res = await request.get("/user/" + id);
      if (res.code === 200) {
        return res.data.userName;
      } else {
        this.$notify.error(res.msg);
      }
    },
    async requestComment(username) {
      const res = await request.get("/comment/page/" + username + "/target", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
        },
      });
      console.log(res);
      this.tableData = res.data.records;
      this.total = res.data.total;
      Promise.all(
        this.tableData.map((row) => this.getUser(row.userAccount))
      ).then((result) => {
        for (let i = 0; i < this.tableData.length; i++) {
          this.$set(this.tableData[i], "useravatar", result[i].avatarUrl);
          this.$set(this.tableData[i], "nikeName", result[i].nikeName);
        }
      });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.load();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNum = val;
      this.load();
    },
    async getUser(account) {
      const res = await request.get("/user/account/" + account);
      if (res.code === 200) {
        return res.data;
      } else {
        this.$notify.error(res.msg);
      }
    },
  },
};
</script>


<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.wrapper {
  position: relative;
  display: flex;
  margin: 5px auto;
  width: 650px;
  overflow: hidden;
  /* border-radius: 8px; */
  padding: 15px;
  /* box-shadow: 5px 4px 14px #bebebe, -20px -20px 60px #ffffff; */
  border-bottom: 1px dashed black;
}
.user {
  position: absolute;
  width: 200px;
  text-align: left;
  margin-top: -25px;
  margin-left: 50px;
}
.avatar {
  margin-left: -47px;
  margin-top: 20px;
}
.username {
  color: #333;
  font-size: 20px;
  font-weight: 700;
  font-family: "Microsoft YaHei";
  margin-left: 15px;
  margin-top: -48px;
}
/* 多行文本超出省略 */
.text {
  font-size: 18px;
  overflow: hidden;
  text-overflow: ellipsis;
  text-align: justify;
  /* display: flex; */
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  position: relative;
  margin-top: 55px;
}
.text::before {
  content: "";
  height: calc(100% - 24px);
  float: right;
}
.text::after {
  content: "";
  width: 999vw;
  height: 999vw;
  position: absolute;
  box-shadow: inset calc(100px - 999vw) calc(30px - 999vw) 0 0 #fff;
  margin-left: -100px;
}
.btn {
  float: right;
  clear: both;
  margin-left: 10px;
  font-size: 16px;
  padding: 0 8px;
  background: #1c92d2;
  line-height: 24px;
  border-radius: 4px;
  color: #fff;
  cursor: pointer;
  /* margin-top: -30px; */
}
.btn::before {
  content: "展开";
}
.exp {
  display: none;
}
.exp:checked + .text {
  -webkit-line-clamp: 999;
}
.exp:checked + .text::after {
  visibility: hidden;
}
.exp:checked + .text .btn::before {
  content: "收起";
}
.nullCss {
  margin: 60px auto;
  font-size: 14px;
  text-align: center;
  color: #999;
}
</style>
